#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "textreport.h"

#include <QMessageBox>
#include <QDebug>
#include <QMenu>
#include <QStandardItemModel>
#include <QFileDialog>

QString headertext =
    "<table width=\"100%\">"
    "  <tr>"
    "    <td align=\"left\"><strong>%1</strong></td>"
    "    <td align=\"right\"><strong>&date;</strong></td>"
    "  </tr>"
    "</table>";

QString footertext = "<p align=\"right\"><strong>&page;</strong></p>";

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    textReport = new YReport::TextReport(this);
    model = new QStandardItemModel(this);
    ui->tableView->setModel(model);
    model->setHorizontalHeaderLabels(QStringList() << "name" << "path");

    ui->tableView->horizontalHeader()->setStretchLastSection(true);


    menu = menuBar()->addMenu("&操作");

    menu->addAction("&N新建", this, SLOT(on_actAdd_triggered()));

    menu->addAction("&P打印", this, SLOT(print()));
    menu->addAction("&E导出为PEF...", this, SLOT(exportPdf()));

    menu->addSeparator();
    menu->addAction("E&xit", this, SLOT(close()));

    connect(ui->plainTextEdit, SIGNAL(textChanged()),
            this, SLOT(documentModified()));

    currentfile_ = QString();
    html_ = false;
    setWindowTitle("[*]Example");
    setWindowModified(false);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actExit_triggered()
{
    qApp->quit();
}

void MainWindow::on_actPerview_triggered()
{
    const QTextDocument *t = ui->plainTextEdit->document();
    textReport->preview(t);
}

void MainWindow::on_tableView_customContextMenuRequested(const QPoint &/*pos*/)
{
    QMenu *menu = new QMenu();

    menu->addAction(ui->actAdd);
    menu->addAction(ui->actDelete);

    menu->exec(this->cursor().pos());
    delete menu;
    menu = Q_NULLPTR;
}

void MainWindow::on_actDelete_triggered()
{
    QModelIndex index = ui->tableView->selectionModel()->currentIndex();
    model->removeRow(index.column());
}


#include <QTextStream>

void MainWindow::on_actAdd_triggered()
{
    QStringList	nameList = QFileDialog::getOpenFileNames(this, "查找文档", "./", "*.txt");
    foreach (const QString &name, nameList) {
        QFileInfo info(name);
        QStandardItem *item1 = new tableItem(info.baseName());
        QStandardItem *item2 = new tableItem(name);
        QList<QStandardItem*> list;
        list << item1 << item2;
        model->appendRow(list);

        QFile file(QDir::fromNativeSeparators(name));
        if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            qDebug() << QString("open file %1 failed").arg(name);
            return;
        }

        QTextStream txtInput(&file);
        while(!txtInput.atEnd()) {
            ui->plainTextEdit->appendPlainText(txtInput.readAll());
        }
        file.close();

    }

}

void MainWindow::closeEvent(QCloseEvent *event)
{
    if (askSave()) event->accept();
    else  event->ignore();
}


void MainWindow::documentModified()
{
    setWindowModified(true);
}

#include "report.h"
void MainWindow::exportPdf()
{
    textReport->setHeaderText(headertext.arg(currentfile_));
    textReport->setFooterText(footertext);
    const QTextDocument *t = ui->plainTextEdit->document();
    textReport->exportPdf(t, QString("导出pdf"));
}

bool MainWindow::askSave()
{
//    if (ui->plainTextEdit->document()->isModified()) {
//        QMessageBox::StandardButton result;
//        result = QMessageBox::warning(this, "Example", "Save changes?",
//                                      QMessageBox::Save |
//                                      QMessageBox::Discard |
//                                      QMessageBox::Cancel);
//        if (result == QMessageBox::Save)
//            return save();
//        else if (result == QMessageBox::Cancel)
//            return false;
//    }
    return true;
}
